﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hi_Baidu.Packet.InPacket.TalkInPacket
{
    /// <summary>
    ///     提取图片请求包
    /// </summary>
    class RequestPicturePacket : TalkMessageInPackage
    {
        public RequestPicturePacket(TalkMessageInPackage inpackage)
            : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //用户名长度
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //好友长度
            byte[] FriendLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, FriendLength, 0, 4);
            Offset += 4;
            //好友
            byte[] Friend = new byte[BitConverter.ToInt32(FriendLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Friend, 0, Friend.Length);
            Offset += Friend.Length;
            //会话ID长度
            byte[] SessionLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, SessionLength, 0, 4);
            Offset += 4;
            //会话ID
            byte[] Session = new byte[BitConverter.ToInt32(SessionLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Session, 0, Session.Length);
            Offset += Session.Length;
            //提取码长度
            byte[] PopCodeLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, PopCodeLength, 0, 4);
            Offset += 4;
            //提取码
            byte[] PopCode = new byte[BitConverter.ToInt32(PopCodeLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, PopCode, 0, PopCode.Length);
            Offset += PopCode.Length;
            //IP长度
            byte[] IPLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            //IP
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            //PID
            byte[] PID = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, PID, 0, 4);
            Offset += 4;
            //好友ID长度
            byte[] TalkPort = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, TalkPort, 0, 4);
            Offset += 4;


            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("FRIENDUSERNAME", Friend);
            this.DIC_CONTENT.Add("TALKSESSION", Session);
            this.DIC_CONTENT.Add("REQUESTPICID", PopCode);
            this.DIC_CONTENT.Add("IPAddress", IPAddress);
            this.DIC_CONTENT.Add("PID", PID);
            this.DIC_CONTENT.Add("TALKPORT", TalkPort);
        }

        /// <summary>
        ///     获得本次会话ID
        /// </summary>
        /// <returns></returns>
        public String GetTalkSession()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["TALKSESSION"]);
        }

        /// <summary>
        ///     获得图片提取码
        /// </summary>
        /// <returns></returns>
        public String GetRequestPicId()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["REQUESTPICID"]).Replace("\"", "").Replace("'", "");
        }

        /// <summary>
        ///     获得图片ID
        /// </summary>
        /// <returns></returns>
        public String GetPID()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["PID"], 0).ToString().Replace("\"", "").Replace("'", "");
        }
    }
}
